/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package servlet;

import ejb.BusinessLogicLocal;
import entity.Campo;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Marco
 */
public class ServletCampo extends HttpServlet {
    @EJB
    private BusinessLogicLocal businessLogic;

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        String op = (String) request.getAttribute("campo");
        if(op.equals("inserisci")){
            int nCampo = Integer.parseInt(request.getParameter("numerocampo"));
            int nSpett = Integer.parseInt(request.getParameter("numerospettatori"));
            String sport = request.getParameter("sport");
            String terreno = request.getParameter("terreno");
            String prezzoCampo = request.getParameter("prezzo");
            float prezzo = Float.parseFloat(prezzoCampo.trim());
            boolean result = businessLogic.insertCampo(nCampo, nSpett, sport, terreno, "Images/"+sport+terreno+".jpg", prezzo);
            if(result){
                RequestDispatcher rd = getServletContext().getRequestDispatcher("/gest_campo.jsp");
                request.setAttribute("campo", "inserito");
                rd.forward(request, response);
            }else{
                //manda pagina di errore
            }
        }else if(op.equals("updateCampo")){
            int nCampo = Integer.parseInt(request.getParameter("numerocampo"));
            int nSpett = Integer.parseInt(request.getParameter("numerospettatori"));
            String sport = request.getParameter("sport");
            String terreno = request.getParameter("terreno");
            Long idCampo = Long.parseLong(request.getParameter("idcampo"));
            String prezzoCampo = request.getParameter("prezzo");
            float prezzo = Float.parseFloat(prezzoCampo.trim());
            boolean result = businessLogic.modificaCampo(idCampo, nCampo, nSpett, sport, terreno, "Images/"+sport+terreno+".jpg" ,prezzo);
            RequestDispatcher rd = getServletContext().getRequestDispatcher("/gest_campo.jsp");
            if(result){
                request.setAttribute("campo", "modificaAvvenuta");
            }else{
                request.setAttribute("campo", "modificaNonAvvenuta");
            }
            rd.forward(request, response);
            
        }else if(op.equals("tabellaCampi")){
            List<Campo> result = businessLogic.getCampi();
            RequestDispatcher rd = getServletContext().getRequestDispatcher("/gest_campo.jsp");
            request.setAttribute("elencocampi", result);
            request.setAttribute("campo", "tabellaCampi");
            rd.forward(request, response);
            
        }else if(op.equals("campiSport")){
            String sport = request.getParameter("sport");
            List<Campo> campi = businessLogic.getCampiBySport(sport);
            RequestDispatcher rd = getServletContext().getRequestDispatcher("/gest_campo.jsp");
            request.setAttribute("campo", "tabellaCampi");
            request.setAttribute("operation", "campiBySport");
            request.setAttribute("elencocampi", campi);
            rd.forward(request, response);
            
        }else if(op.equals("prenotazioneCampo")){
            String nCampo = request.getParameter("nCampo");
            String idCampo = request.getParameter("idcampo");
            String ora = request.getParameter("oracampo");
            String data = request.getParameter("datepick2");
            String nomeUtente = request.getParameter("nomeutente");
            long idUtente = businessLogic.getIdUtente(nomeUtente);
            boolean prenotazione = businessLogic.prenotaCampo(Long.parseLong(idCampo), idUtente, data, ora, nCampo);
            RequestDispatcher rd = getServletContext().getRequestDispatcher("/gest_campo.jsp");
            if(prenotazione){
                request.setAttribute("prenotazione", "avvenuta");
                HttpSession session = request.getSession();
                Campo c = businessLogic.getCampo(Long.decode(idCampo));
                String messaggio = "Salve " + (String) session.getAttribute("nomeUtente") + ",<br> desideriamo informarti che la prenotazione per il campo: " + nCampo + " "+ c.getSport() + ", in data: " + data + " " + ora + " è andata a buon fine.<br><br>Cordiali Saluti,<br>mfn-centrosportivo";
                businessLogic.sendMail((String) session.getAttribute("nomeUtente"), "Campo prenotato", messaggio," "," ");
            }else{
                request.setAttribute("prenotazione", "nonavvenuta");
            }
            request.setAttribute("campo", "prenotato");
            rd.forward(request, response);
        }
        
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
